package com.gk.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.study.service.AdminService;
import com.gk.study.entity.Admin;
import com.gk.study.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<Admin> getAdminList(String keyword) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(keyword)){
            // like查询
            queryWrapper.like("username", keyword);
        }
        queryWrapper.orderBy(true, false, "create_time");
        return adminMapper.selectList(queryWrapper);
    }

    @Override
    public Admin getAdminByUserName(String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Admin getAdminByToken(String token) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token", token);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public void createAdmin(Admin admin) {
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.insert(admin);
    }

    @Override
    public void deleteAdmin(String id) {
        adminMapper.deleteById(id);
    }

    @Override
    public void updateAdmin(Admin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.updateById(admin);
    }

    @Override
    public Admin getAdminDetail(String adminId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", adminId);
        return adminMapper.selectOne(queryWrapper);
    }
} 